class code5_2 {
    int m;
    int n;
    int[] dx = {1,-1,0,0};
    int[] dy = {0,0,1,-1};
    public int maxAreaOfIsland(int[][] grid) {
        m = grid.length;
        n = grid[0].length;
        int ret = 0;

        for(int i=0;i<m;i++) {
            for(int j=0;j<n;j++) {
                if(grid[i][j]==1) {
                    ret = Math.max(ret,bfs(grid,i,j));
                }
            }
        }
        return ret;
    }

    public int bfs(int[][] grid,int i,int j) {
        Queue<int[]> q = new LinkedList<>();
        q.offer(new int[]{i,j});
        grid[i][j] = 2;
        int area = 1;
        while (!q.isEmpty()) {
            int[] x = q.poll();
            for(int index=0;index<4;index++) {
                int x1 = x[0]+dx[index];
                int y1 = x[1]+dy[index];

                if(x1>=0 && x1<m && y1>=0&& y1<n&&grid[x1][y1]==1) {
                    grid[x1][y1] = 2;
                    area++;
                    q.offer(new int[]{x1,y1});
                }
            }
        }
        return area;
    }


}